//Page Object
const { request} = require('../../utils/request')
Page({
  data: {
    title:'',
    content:'',
    currentRecord:null,
    emptyVoiceDialogVisible: false,
  },
  //options(Object)
  onLoad: function(options){
    let recordId = options.id;
    if(recordId){
      this.setData({
        recordId:recordId
      })
      this.getCurRecordData(recordId);
    }else {
      wx.showModal({
        title: '提示',
        content: '记录id不能为空',
        showCancel: false,
        confirmText: '确定',
        success: (res) => {
          this.jumpBackPage()
        }
      })
    }
  },
 
  onShow: function(){
    wx.setStorageSync('updateRecord',false)
  },
  titleInput(event:any){
    this.setData({
      title:event.detail.value
    })
  },
  textAreaInput(event:any){
    this.setData({
      content:event.detail.value
    })
  },
  jumpBackPage() {
    wx.navigateBack({
      delta: 1
    })
  },
  updateRecordData(){
    if(!this.data.title){
      wx.showToast({
        title: '标题不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    // if(this.data.title.length > 6){
    //   wx.showToast({
    //     title: '标题不能超过6个字',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return;
    // }
    if(!this.data.content){
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    setTimeout(()=>{
      request({
        url:`/records/${this.data.currentRecord.id}/content`,
        method:'POST',
        data:{
          title:this.data.title,
          content:this.data.content
        },
        success(rData:any){
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000,
            success(){
              wx.setStorageSync('updateRecord',true)
              wx.navigateBack({
                delta: 1
              })
              // wx.redirectTo({
              //   url: '/pages/recordResult/recordResult?id='+this.data.currentRecord.id+'&update=1'
              // })
            }
          })
        }
      })
    },100)
    
  },
  showEmptyVoiceDialog(){
    this.setData({
      emptyVoiceDialogVisible: true
    })
  },
  hideEmptyVoiceDialog(){
    this.setData({
      emptyVoiceDialogVisible: false
    })
  },
  getCurRecordData(recordId:string){
    let that = this as any
    wx.showLoading({
      title: '加载中',
    })
    request({
      url:'/records/'+recordId,
      success(rData:any){
        // console.log('rData:',rData)
        // 已完成
        if(rData.status ===2 ){
          // console.log('rData content:',rData.content)
          that.setData({
            currentRecord:rData,
            title:rData.title,
            content:rData.content
          })
        }else if(rData.status ===3){
          // 内容为空
          that.showEmptyVoiceDialog()
          setTimeout(()=>{
            that.hideEmptyVoiceDialog()
            that.jumpBackPage()
          },2000)
          
          
        }
      },
      complete(){
        wx.hideLoading()
      }
    })
    
  },
  
});